﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// address 的摘要说明
    /// </summary>
    public class address : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            BLL.province bllpro = new BLL.province();
            BLL.city bllcity = new BLL.city();
            BLL.district blldis = new BLL.district();
            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("[");
            DataSet prolist = bllpro.GetList(999,"1=1"," id asc");
            int proi = 0;
            int _procount = prolist.Tables[0].Rows.Count;
            for (int ii = 0; ii < _procount; ii++)
            {
                DataRow prodr = prolist.Tables[0].Rows[ii];
                // foreach (DataRow prodr in prolist.Tables[0].Rows) {
                strTxt.Append("{");
                strTxt.Append("\"id\":\""+prodr["id"]+"\",");
                strTxt.Append("\"name\":\"" + prodr["province_name"] + "\"");
                //获取城市列表
                DataSet citylist = bllcity.GetList(999, " province_id=" + prodr["id"], " id asc");
                if (citylist.Tables[0].Rows.Count > 0) {
                    int _citycount = citylist.Tables[0].Rows.Count;
                    strTxt.Append(",\"sub\":[");
                    //foreach (DataRow citydr in citylist.Tables[0].Rows) {
                    for (int jj = 0; jj < _citycount; jj++) {
                        DataRow citydr= citylist.Tables[0].Rows[jj];
                        strTxt.Append("{");
                        strTxt.Append("\"id\":\"" + citydr["id"] + "\",");
                        strTxt.Append("\"name\":\"" + citydr["city_name"] + "\"");
                        //获取乡镇列表
                        DataSet dislist = blldis.GetList(999, " city_id=" + citydr["id"], "id asc");
                        if (dislist.Tables[0].Rows.Count > 0) {
                            int _discount = dislist.Tables[0].Rows.Count;
                            strTxt.Append(",\"sub\":[");
                            for (int kk = 0; kk < _discount; kk++)
                            {
                                DataRow disdr = dislist.Tables[0].Rows[kk];
                                strTxt.Append("{");
                                strTxt.Append("\"id\":\"" + disdr["id"] + "\",");
                                strTxt.Append("\"name\":\"" + disdr["district_name"] + "\"");
                                strTxt.Append("}");
                                if (kk < _discount - 1)
                                {
                                    strTxt.Append(",");
                                }
                            }

                             strTxt.Append("]");
                        }
                        strTxt.Append("}");
                        if (jj < _citycount - 1)
                        {
                            strTxt.Append(",");
                        }
                    }


                    

                    strTxt.Append("]");
                }

                strTxt.Append("}");
                if (ii < _procount - 1)
                {
                    strTxt.Append(",");
                }
            }

            strTxt.Append("]");
            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}